package com.aike.service;

import com.aike.dao.GradeMapper;
import com.aike.dao.UserMapper;
import com.aike.pojo.*;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GradeServiceImpl implements GradeService {

    private GradeMapper gradeMapper;

    private UserMapper userMapper;

    public void setGradeMapper(GradeMapper gradeMapper) {
        this.gradeMapper = gradeMapper;
    }

    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    public String getGradeList(Integer userId) {
        JSONObject object = new JSONObject();
        Map<String, Object> map = new HashMap<String, Object>();
        User user = userMapper.getUserById(userId);
        map.put("user", user);
        if(user.getType() == 1)
        {
            List<GradeListForStudent> list = gradeMapper.getGradeListForStudent(userId);
            map.put("list", list);
        }
        else if(user.getType() == 2)
        {
            List<GradeListForTeacher> list = gradeMapper.getGradeListForTeacher(userId);
            map.put("list", list);
        }
        else
        {
            List<GradeListForAdministrator> list = gradeMapper.getGradeListForAdministrator();
            map.put("list", list);
        }

        object.put("data", map);
        object.put("meta", new Meta(200, "获取成功"));

        return object.toJSONString();
    }

    public String getGradeShow(Integer userId, Integer gradeId) {
        JSONObject object = new JSONObject();
        Map<String, Object> map = new HashMap<String, Object>();
        User user = userMapper.getUserById(userId);
        map.put("user", user);
        if(user.getType() == 2 || user.getType() == 3)
        {
            GradeShowForTeacher gradeShowForTeacher = gradeMapper.getGradeShowForTeacher(gradeId);
            map.put("show", gradeShowForTeacher);
        }

        object.put("data", map);
        object.put("meta", new Meta(200, "获取成功"));

        return object.toJSONString();
    }

    public String saveGrade(Integer gradeId, Integer gradeNum) {
        JSONObject object = new JSONObject();
        int v = gradeMapper.saveGrade(gradeId, gradeNum);
        if(v > 0)
        {
            object.put("data", "null");
            object.put("meta", new Meta(200, "修改成功"));
        }
        else
        {
            object.put("data", "null");
            object.put("meta", new Meta(400, "修改失败"));
        }

        return object.toJSONString();
    }

    public String addNewTaskAnswer(Integer userId, Integer taskId, String gradeAnswer) {
        JSONObject object = new JSONObject();
        int v = gradeMapper.addNewTaskAnswer(userId, taskId, gradeAnswer);
        if(v > 0)
        {
            object.put("data", "null");
            object.put("meta", new Meta(200, "增加成功"));
        }
        else
        {
            object.put("data", "null");
            object.put("meta", new Meta(400, "增加失败"));
        }

        return object.toJSONString();
    }
}
